﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 视频大小估算
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label13.Text = "Ver:" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }
        //重置
        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text = "128";
            textBox1.Text = textBox3.Text= textBox4.Text= textBox5.Text= label11.Text="";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Decimal video = convertInt(textBox1.Text);
            Decimal audio = convertInt(textBox2.Text);
            Decimal h = convertInt(textBox3.Text)*60*60;//小时
            Decimal m = convertInt(textBox4.Text)*60;//分钟
            Decimal s = convertInt(textBox5.Text);//秒
            Decimal total_second = h + m + s;
            Decimal total_bits = video + audio;
            Decimal final = total_bits/1024*total_second/8;
            Decimal final_GB = final / 1024;
            Decimal swf_limit = 2;
            Decimal result = swf_limit - final_GB;
            if (result < 0)
            {
                label11.Text = "该视频约" + final.ToString("0.00") + "MB,估计占用空间" + final_GB.ToString("0.00") + "GB,已经超过网站上传上限2GB，请调整参数重新压缩视频";
            }
            Decimal remainder = Convert.ToDecimal("0.20");
            if (result>0 && result > remainder)
            {
                label11.Text = "该视频约" + final.ToString("0.00") + "MB,估计占用空间" + final_GB.ToString("0.00") + "GB,未过网站上传上限2GB，可尝试上传";
            }
            if (result > 0 && result <= remainder)
            {
                label11.Text = "该视频约" + final.ToString("0.00") + "MB,估计占用空间" + final_GB.ToString("0.00") + "GB,已接近网站上传上限2GB，建议整参数重新压缩视频";
            }

        }
        //限制只能输入数字
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (!IsNumeric(textBox1.Text))
            {
                textBox1.Text = "";
            }
        }

        //转换格式，空和小于0则为0
        public static Decimal convertInt(string value)
        {
            if(string.IsNullOrWhiteSpace(value) || string.IsNullOrEmpty(value)){
                return 0;
            }
            else
            {
                int num = Convert.ToInt32(value);
                if (num < 0)
                {
                    return 0;
                }
                else
                {
                    return num;
                }
            }
        }
        //判断是否数字
        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (!IsNumeric(textBox2.Text))
            {
                textBox2.Text = "128";
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (!IsNumeric(textBox3.Text) || convertInt(textBox3.Text) > 24 || convertInt(textBox3.Text)<0)
            {
                textBox3.Text = "";
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (!IsNumeric(textBox4.Text) || convertInt(textBox4.Text)> 59 || convertInt(textBox4.Text) < 0)
            {
                textBox4.Text = "";
            }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if (!IsNumeric(textBox5.Text) || convertInt(textBox5.Text) > 59 || convertInt(textBox5.Text) < 0)
            {
                textBox5.Text = "";
            }
        }

        



        //private void label9_Click(object sender, EventArgs e)
        //{

        //}

        //private void label1_Click(object sender, EventArgs e)
        //{

        //}

        //private void label2_Click(object sender, EventArgs e)
        //{

        //}

        //private void label3_Click(object sender, EventArgs e)
        //{

        //}

        //private void label4_Click(object sender, EventArgs e)
        //{

        //}

        //private void label5_Click(object sender, EventArgs e)
        //{

        //}

        //private void textBox2_TextChanged(object sender, EventArgs e)
        //{

        //}

        //private void label6_Click(object sender, EventArgs e)
        //{

        //}

        //private void label8_Click(object sender, EventArgs e)
        //{

        //}
    }
}
